import { Context } from 'egg';

export default function verifyTokenMiddleware(): any {
  return async (ctx: Context, next: () => Promise<any>) => {
    const { authorization } = ctx.request.headers;
    if (authorization) {
      const data: any = ctx.app.jwt.decode(authorization.toString());
      if (data && !ctx.helper.isExpire(data.exp)) {
        await next();
      } else {
        ctx.body = { code: 401, message: 'token已经过期', data: {} };
        ctx.status = 401;
      }
    }
  };
}
